﻿/*eslint-disable*/
/**
 * 预览视频功能
 */
import axios from 'axios'
class PreviewOCX {
	constructor(options) {
		this.SUCCESS = 0; /* 控件操作成功*/
		this.FAILED = 1; /* 控件操作失败*/
		this.ERROR = -1; /* 控件操作错误*/
		this.PTZ_CMD_FOCUS_NEAR = 13 /* 焦点以速度SS前调*/
		this.PTZ_CMD_FOCUS_FAR = 14 /* 焦点以速度SS后调*/
		this.PTZ_CMD_START = 0; /* 开始命令*/
		this.PTZ_CMD_STOP = 1; /* 结束命令*/
		this.PTZ_CMD_TILT_UP = 21 /* 云台以SS的速度上仰*/
		this.PTZ_CMD_TILT_DOWN = 22 /* 云台以SS的速度下俯*/
		this.PTZ_CMD_PAN_LEFT = 23 /* 云台以SS的速度左转*/
		this.PTZ_CMD_PAN_RIGHT = 24 /* 云台以SS的速度右转*/
		this.PTZ_CMD_ZOOM_IN = 11 /* 焦距以速度SS变大(倍率变大) */
		this.PTZ_CMD_ZOOM_OUT = 12 /* 焦距以速度SS变小(倍率变小) */
		this.PTZ_CMD_IRIS_OPEN = 15 /* 光圈以速度SS扩大*/
		this.PTZ_CMD_IRIS_CLOSE = 16 /* 光圈以速度SS缩小*/
		this.PRESET_CMD_SET_PRESET = 8 /* 设置预置点*/
		this.PRESET_CMD_CLE_PRESET = 9 /* 清除预置点*/
		this.PRESET_CMD_GOTO_PRESET = 39 /* 转到预置点*/
		// 抓图结束通知
		this.PREVIEW_EVENT_SNAP = 33554433;
		// 紧急录像结束通知
		this.PREVIEW_EVENT_VEDIO = 33554434;
		// 开始预览通知
		this.PREVIEW_EVENT_START = 33554437;
		// 停止预览通知
		this.PREVIEW_EVENT_STOP = 33554438;
		// 控件初始化完毕消息
		this.PREVIEW_EVENT_INIT_OK = 33554439;
		// 获取token消息通知
		this.PREVIEW_EVENT_TOKEN = 33554446;
		// 窗口选中改变通知
		this.PREVIEW_EVENT_SELECTED = 33554449;
		// 窗口交换改变
		this.PREVIEW_EVENT_CHANGE = 33554450;
		// 预览控件
		this.previewOCX;
		// 窗口数量
		this.windowNumber = 4;
		// 存放预览控件回调函数的数组
		this.callback = [];
		// 容器id
		this.divId = options.divId;
		// 主题
		this.theme = options.theme;
		this.tokenCallEvent = (reqID) => {}
		// 将预览事件放置全局
		window.EventNotify = this.EventNotify.bind(this)
		window.OnfireEventNotify = this.OnfireEventNotify.bind(this)
		// 初始化
		this.init()

	}
	/**
	 * 初始化
	 */
	init() {

		// 获取控件所在DIV
		var divObj = document.getElementById(this.divId);
		if (this.isBrowserIE() == 2) { // IE11
			// 加载控件
			var o = document.createElement("object");
			var o2 = document.createElement("PARAM");
			o2.setAttribute("NAME", "THEME");
			o2.setAttribute("VALUE", this.theme);
			o.setAttribute("classid",
				"clsid:6DABF69C-7610-4D25-BC4F-664003130B94");
			o.setAttribute("width", "100%");
			o.setAttribute("height", "100%");
			o.setAttribute("id", "PreviewOCX");
			o.setAttribute("name", "PreviewOCX");
			o.appendChild(o2);
			//			o.object.Theme =this.theme;
			divObj.appendChild(o);

			var script = document.createElement("script");
			script.setAttribute("language", "javascript");
			script.setAttribute("for", "PreviewOCX");
			script.setAttribute("event",
				"EventNotify(event_type, wnd_index, param)");
			script.innerHTML = "EventNotify(event_type, wnd_index, param);";
			divObj.appendChild(script);
		} else if (this.isBrowserIE() == 1) { //chrome
			// divObj.innerHTML = '<param name="wmode" value="transparent" /><embed id="PreviewOCX"  wmode="transparent" style="z-index:-1;" type="application/npPlatformPreview-plugin"   name="PreviewOCX" width="100%" height="100%"></embed>';
			divObj.innerHTML = '<embed id="PreviewOCX"  wmode="transparent" style="z-index:-1;" type="application/npPlatformPreview-plugin"   name="PreviewOCX" width="100%" height="100%"></embed>';
		} else {
			if (-[1, ]) { // IE9,IE10
				//加载控件
				var object = document.createElement("object");
				object.setAttribute("classid",
					"clsid:6DABF69C-7610-4D25-BC4F-664003130B94");
				object.setAttribute("width", "100%");
				object.setAttribute("height", "100%");
				object.setAttribute("id", "PreviewOCX");
				object.setAttribute("name", "PreviewOCX");
				object.setAttribute("Theme", this.theme);
				//object.object.Theme =this.theme;
				divObj.appendChild(object);

				var script = document.createElement("script");
				script.setAttribute("language", "javascript");
				script.setAttribute("for", "PreviewOCX");
				script.setAttribute("event",
					"EventNotify(event_type, wnd_index, param)");
				script.innerHTML = "EventNotify(event_type, wnd_index, param);";
				divObj.appendChild(script);
				//				divObj.innerHTML = '<embed id="PreviewOCX" type="application/PlatformPreviewDll-plugin"  name="PreviewOCX" width="100%" height="100%"></embed>';
			} else { // IE6,7,8
				divObj.innerHTML = '<object id=\"PreviewOCX\"skin=\"' + this.skin + '\" classid=\"clsid:6DABF69C-7610-4D25-BC4F-664003130B94\" width="100%" height="100%" ><param name="Theme" value =\"' + this.theme + '\"></object>' +
					'<script language=\"javascript\" for=\"PreviewOCX\" event=\"EventNotify(event_type, wnd_index, param)\">EventNotify(event_type, wnd_index, param);</script>';
			}
		}
		// 预览控件
		this.previewOCX = document.getElementById("PreviewOCX");
	}
	/**
	 * 设置全局参数
	 * 
	 * @param:xml 全局参数
	 * @returns:操作发起成功0；失败返回错误码1-没有方法
	 */
	setGlobalParam(xml) {
		try {
			var result = this.previewOCX.SetGlobalParam(xml);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 设置操作权限
	 * 
	 * @param:operXml 权限类型
	 * @returns:成功返回0；失败返回错误码
	 */
	setOperAuth(operXml) {
		try {
			var result = this.previewOCX.SetOpPrivilege(operXml);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 设置Token
	 * 
	 * @param:reqID 令牌请求ID
	 * @param:token 令牌
	 * @returns:成功返回0；失败返回错误码
	 */
	setToken(reqID, token) {
		try {
			var result = this.previewOCX.SetToken(reqID, token);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 设置窗口布局
	 * 
	 * @param:layoutType 布局格式枚举见示例
	 * @returns:成功返回0；失败返回错误码
	 * /窗口布局 1=》1*1；2=》2*2；3=》3*3；5=》4*4；
	 */
	setWindowsLayout(layoutType) {
		try {
			let obj = {
				1: 1,
				2: 4,
				3: 9,
				5: 16,

			}
			var result = this.previewOCX.SetLayoutType(layoutType);
			this.windowNumber = obj(layoutType)
			console.log('窗口设置成功' + layoutType)
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 开始预览
	 * 
	 * @param:xml 预览参数
	 * @param:wndIdx 当前预览的窗口索引号
	 * @returns:操作发起成功0；失败返回错误码
	 */
	startPreview(wndIdx, xml) {
		try {
			var result = this.previewOCX.StartPreview(xml, wndIdx);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 停止预览
	 * 
	 * @param:index 窗口索引，-1表示停止所有窗口的预览
	 * @returns:操作发起成功0；失败返回错误码
	 */
	stopPreview(index) {
		try {
			var result = this.previewOCX.StopPreview(index);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 语音对讲
	 * @param:bOpen 1代表打开 0代表关闭
	 * @param:wndIndex 窗口索引
	 * @returns:操作发起成功0；失败返回错误码
	 */
	DeviceTalk(wndIndex, bOpen) {
		try {
			var result = this.previewOCX.DeviceTalk(wndIndex, bOpen);

			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 打开声音
	 * @param:bOpen 1代表打开 0代表关闭
	 * @returns:操作发起成功0；失败返回错误码
	 */
	PlaySound(bOpen) {
		try {
			var result = this.previewOCX.PlaySound(bOpen);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 云镜控制
	 * 
	 * @param:cmdId 云台控制参数
	 * @param:xml 云台控制参数
	 * @returns:操作发起成功0；失败返回错误码
	 */
	ptzControl(cmdId, xml) {
		try {
			var result = this.previewOCX.PtzControl(cmdId, xml);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 视频参数设置
	 * 
	 * @param:xml 设置的视频参数
	 * @returns:成功返回0；失败返回错误码
	 */
	setPreviewParam(xml) {
		try {
			var result = this.previewOCX.SetVideoParam(xml);
			return result;
		} catch (e) {
			return 1;
		}
	}
	/**
	 * 视频参数获取
	 * 
	 * @returns:视频参数
	 */
	getPreviewParam() {
		try {
			var cfgXml = this.previewOCX.GetVideoParam();
			return cfgXml;
		} catch (e) {
			return "";
		}
	}
	/**
	 * 预览状态获取
	 * 
	 * @param:wndIndex 指定窗口的窗口索引值
	 * @returns:0表示没有预览，1表示正在预览
	 */
	getPreviewStatus(wndIndex) {
		try {
			var status = this.previewOCX.IsPreview(wndIndex);
			return status;
		} catch (e) {
			return 0;
		}
	}
	// 设置token验证函数
	setTokenCallEvent(fn) {
		this.tokenCallEvent = fn
	}
	/**
	 * 设置当前选中窗口
	 * 
	 * @param:wndIndex 指定窗口的窗口索引值
	 * @returns:操作发起成功0；失败返回错误码
	 */
	setSelectWindow(wndIndex) {
		try {
			var result = this.previewOCX.SelectWindow(wndIndex);
			return result;
		} catch (e) {
			return -1;
		}
	}
	// 设置回调函数
	setCallBack() {
		//下面的回调函数除了token回调，其他都是可选的，用户选取自己的业务需要做适当的增删方法；
		//设置token回调
		this.registerCallback(this["PREVIEW_EVENT_TOKEN"], this.tokenCallEvent);
		// //抓图回调函数
		this.registerCallback(this["PREVIEW_EVENT_SNAP"], this.picCutEvent);
		// //录像回调
		this.registerCallback(this["PREVIEW_EVENT_VEDIO"], this.videoCutEvent);
		// //开始预览回调
		this.registerCallback(this["PREVIEW_EVENT_START"], this.startPreviewEvent);
		// //停止预览回调
		this.registerCallback(this["PREVIEW_EVENT_STOP"], this.stopPreviewEvent);
		// //窗口选中改变回调
		this.registerCallback(this["PREVIEW_EVENT_SELECTED"], this.windowChangeEvent);
		// // 窗口交换改变
		this.registerCallback(this["PREVIEW_EVENT_CHANGE"], this.windowExchangeEvent);
		// 控件初始化完毕消息
		this.registerCallback(this["PREVIEW_EVENT_INIT_OK"], this.windowInitOK);
	}
	//窗口交换改变事件通知
	windowExchangeEvent(index) {
		console.log("窗口交换改变，当前窗口索引为：" + index + "句柄" + this.getSelectWindow());
	}
	//抓图完成事件通知
	picCutEvent(param) {
		var windIndex = this.getSelectWindow();
		if (param === this.SUCCESS) {
			console.log("窗口" + windIndex + "：抓图成功");
		} else {
			console.log("窗口" + windIndex + "：抓图失败");
		}
	}
	//录像完成事件通知
	videoCutEvent(param) {
		var windIndex = this.getSelectWindow();
		if (param === this.SUCCESS) {
			console.log("窗口" + windIndex + "：录像成功");
		} else {
			console.log("窗口" + windIndex + "：录像失败");
		}
	}
	//开始预览事件通知
	startPreviewEvent() {
		var windIndex = this.showptz();
		console.log("窗口" + windIndex + "：开始预览");
	}
	//显示云台控制
	showptz() {
		var windIndex = this.getSelectWindow();
		if (this.getPreviewStatus(windIndex) == 1) {
			// videoParameterUpdate();
		}
		return windIndex;
	}
	//停止预览事件通知
	stopPreviewEvent() {
		var windIndex = this.getSelectWindow();
		console.log("窗口" + windIndex + "：停止预览");
	}
	//窗口选择改变事件通知
	windowChangeEvent(index) {
		var windIndex = this.showptz();
		console.log("窗口选中改变，当前窗口索引为：" + windIndex);
	}
	//云台移动，变焦
	ptzControllerMove(command, action) {
		console.log(command, action);
		this.showptz();
		var xml = [];
		xml.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		xml.push("<PtzControl>");
		xml.push("<Action>" + action + "</Action>");
		xml.push("<Priority>50</Priority>");
		xml.push("<Speed>5</Speed>");
		//xml.push("<Preset Index =1 DwellTime=50 Speed=5 />");
		xml.push("</PtzControl>");
		if (this.ptzControl(command, xml.join("")) === this.SUCCESS) {
			console.log("云台操作：" + command + ",操作：" + action + "成功");
		} else {
			console.log("云台操作：" + command + ",操作：" + action + "失败");
		}
	}
	// 云台上仰开始01
	controlUpStart() {
		this.ptzControllerMove(this.PTZ_CMD_TILT_UP, this.PTZ_CMD_START);
	}
	// 云台上仰停止02
	controlUpStop() {
		this.ptzControllerMove(this.PTZ_CMD_TILT_UP, this.PTZ_CMD_STOP);
	}
	// 云台下俯开始03
	controlDownStart() {
		this.ptzControllerMove(this.PTZ_CMD_TILT_DOWN, this.PTZ_CMD_START);
	}
	// 云台下俯停止04
	controlDownStop() {
		this.ptzControllerMove(this.PTZ_CMD_TILT_DOWN, this.PTZ_CMD_STOP);
	}
	// 云台左移开始05
	controlLeftStart() {
		this.ptzControllerMove(this.PTZ_CMD_PAN_LEFT, this.PTZ_CMD_START);
	}
	// 云台左移停止06
	controlLeftStop() {
		this.ptzControllerMove(this.PTZ_CMD_PAN_LEFT, this.PTZ_CMD_STOP);
	}
	// 云台右移开始07
	controlRightStart() {
		this.ptzControllerMove(this.PTZ_CMD_PAN_RIGHT, this.PTZ_CMD_START);
	}
	// 云台右移停止08
	controlRightStop() {
		this.ptzControllerMove(this.PTZ_CMD_PAN_RIGHT, this.PTZ_CMD_STOP);
	}
	// 倍率变大开始09
	controlZoomInStart() {
		this.ptzControllerMove(this.PTZ_CMD_ZOOM_IN, this.PTZ_CMD_START);
	}
	// 倍率变大停止10
	controlZoomInStop() {
		this.ptzControllerMove(this.PTZ_CMD_ZOOM_IN, this.PTZ_CMD_STOP);
	}
	// 倍率变小开始11
	controlZoomOutStart() {
		this.ptzControllerMove(this.PTZ_CMD_ZOOM_OUT, this.PTZ_CMD_START);
	}
	// 倍率变小停止12
	controlZoomOutStop() {
		this.ptzControllerMove(this.PTZ_CMD_ZOOM_OUT, this.PTZ_CMD_STOP);
	}
	// 焦点前调开始13
	controlFocusNearStart() {
		this.ptzControllerMove(this.PTZ_CMD_FOCUS_NEAR, this.PTZ_CMD_START);
	}
	// 焦点前调停止14
	controlFocusNearStop() {
		this.ptzControllerMove(this.PTZ_CMD_FOCUS_NEAR, this.PTZ_CMD_STOP);
	}
	// 焦点后调开始15
	controlFocusFarStart() {
		this.ptzControllerMove(this.PTZ_CMD_FOCUS_FAR, this.PTZ_CMD_START);
	}
	// 焦点后调停止16
	controlFocusFarStop() {
		this.ptzControllerMove(this.PTZ_CMD_FOCUS_FAR, this.PTZ_CMD_STOP);
	}
	// 光圈扩大开始17
	controlIrisOpenStart() {
		this.ptzControllerMove(this.PTZ_CMD_IRIS_OPEN, this.PTZ_CMD_START);
	}
	// 光圈扩大停止18
	controlIrisOpenStop() {
		this.ptzControllerMove(this.PTZ_CMD_IRIS_OPEN, this.PTZ_CMD_STOP);
	}
	// 光圈缩小开始19
	controlIrisCloseStart() {
		this.ptzControllerMove(this.PTZ_CMD_IRIS_CLOSE, this.PTZ_CMD_START);
	}
	// 光圈缩小停止20
	controlIrisCloseStop() {
		this.ptzControllerMove(this.PTZ_CMD_IRIS_CLOSE, this.PTZ_CMD_STOP);
	}
	/**
	 * 获取当前选中窗口
	 * 
	 * @returns:选中窗口索引
	 */
	getSelectWindow() {
		try {
			var index = this.previewOCX.GetCurrentSelectWindow();
			return index;
		} catch (e) {
			return 0;
		}
	}
	/**
	 * 注册回调方法
	 * 
	 * @param:callbackType 回调类型
	 * @param:fn 回调方法
	 */
	registerCallback(callbackType, fn) {
		this.callback[callbackType] = fn;
	}
	//预览消息事件，9800 v4.2版本，需要支持chrome浏览器时才使用
	OnfireEventNotify(EventType, WndIndex, EventXml) {
		var RealTimePlayOcx = document.getElementById("PreviewOCX");
		switch (EventType) {
			case 0x02000001: //抓图
				break;
			case 0x02000002: //紧急录像
				break;
			case 0x02000005: //开始预览
				break;
			case 0x02000006: //停止预览
				//alert(ocx.getPreviewStatus(windIndex));
				//alert(ocx.getPreviewStatus(0));
				// let windIndex = ocx.getSelectWindow();
				// if (this.PreviewOCX.getPreviewStatus(windIndex) == 0) {
				//   // indexcodes[windIndex] = "0";
				// }

				break;
			case 0x0200000e: //获取令牌
				//var reqId = new new Number(EventXml);
				// axios.get('/video/robot/getTokens')
				// $.ajax({
				//   url: "../rest/robot/getTokensMuti",
				//   type: "post",
				//   data: { bdzId: selectedBdz },
				//   dataType: "text",
				//   success: function (token) {
				//     console.log(new Date() + ":视频插件赋值");
				//     RealTimePlayOcx.SetToken(EventXml, token);
				//   },
				// });
				this.tokenCallEvent(EventXml);
				break;
			case 0x02000011: //窗口选中改变
				break;
			case 0x02000007: //控件初始化完毕事件
				break;
			case 0x02000012: //窗口交换改变
				break;
			default: //
		}
	}
	/**
	 * 预览事件消息通知
	 * 
	 * @param:event_type 事件类型
	 * @param:wnd_index 窗口索引
	 * @param:xml 事件携带的参数
	 */
	EventNotify(event_type, wnd_index, xml) {
		switch (event_type) {
			case this.PREVIEW_EVENT_SNAP:
				if (this.callback[event_type]) {
					this.callback[event_type].call(this, xml);
				}
				break;
			case this.PREVIEW_EVENT_VEDIO:
				if (this.callback[event_type]) {
					this.callback[event_type].call(this, xml);
				}
				break;
			case this.PREVIEW_EVENT_START:
				if (this.callback[event_type]) {
					this.callback[event_type].call();
				}
				break;
			case this.PREVIEW_EVENT_STOP:
				if (this.callback[event_type]) {
					this.callback[event_type].call();
				}
				break;
			case this.PREVIEW_EVENT_INIT_OK:
				if (this.callback[event_type]) {
					this.callback[event_type].call();
				}
				break;
			case this.PREVIEW_EVENT_TOKEN:
				if (this.callback[event_type]) {
					this.callback[event_type].call(this, xml);
				}
				break;
			case this.PREVIEW_EVENT_SELECTED:
				if (this.callback[event_type]) {
					this.callback[event_type].call(this, wnd_index);
				}
				break;
			case this.PREVIEW_EVENT_CHANGE:
				if (this.callback[event_type]) {
					this.callback[event_type].call(this, wnd_index);
				}
				break;
			default:
				// alert("视频预览通知消息类型错误！");
		}
	}
	/**
	 * 判断浏览器类型
	 * 
	 * @returns:0-IE8~IE10,1-Chrome,2-IE11
	 */
	isBrowserIE() {
		var appName = navigator.userAgent;
		if (appName.indexOf('MSIE') > -1) {
			return 0;
		} else if (appName.indexOf('Chrome') > -1) {
			return 1;
		} else if (!!window.ActiveXObject || "ActiveXObject" in window) {
			// 判断是否是IE11
			return 2;
		}
		return 0;
	}
}


export default PreviewOCX